'use strict';

/*var app = angular.module('app');*/
angular.module('app').controller('downloadHouseController', [ '$rootScope', '$scope', '$http', '$state','toaster','$modal',
	                                function($rootScope, $scope, $http, $state,toaster,$modal) {
	$scope.title = '数据导入导出';
	$scope.time;
	$scope.param={};
	$scope.submitBtn = true;
	$scope.codeNotpass = false;
	$scope.upload = false;
	
	var pass = sessionStorage.getItem('pass');
	if(pass){
//		console.log(pass);
		$scope.pass = false;
	}else{
		$scope.pass = true;
	}
	//获取用户手机号码
	$scope.userPhone = function(){
		$scope.phone = '手机号码获取中...';
		$.ajax({
			type: 'POST',
            dataType: 'json',
			contentType:'application/json;charset=UTF-8',
			url : 'dac/userManager/queryUser',
			data: angular.toJson($scope.param)
		}).then(function(result){
            $scope.loading = false;
			if(result.httpCode ==200){//成功
                $scope.phone = result.data.phone_;
                $scope.$apply();
            }else{
            	$scope.phone = '';
                toaster.clear('*');
                toaster.pop('error', '', result.msg);
            }
		});
		
	};
	$scope.userPhone();

	$scope.getSmsCode = function (phone) {
		
		$scope.record = {};
	    $scope.record.phone =  phone;
			$.ajax({
    			type: 'POST',
                dataType: 'json',
    			contentType:'application/json;charset=UTF-8',
    			url : '/user/forgetPassword/getSmsCode',
    			data: angular.toJson($scope.record)
    		}).then(function (result) {
                $scope.loading = false;
                if (result.httpCode == 200) {
                	toaster.clear('*');
                    toaster.pop('success', '', "验证码已发送，请注意接收！");
                    var obj = $('#getCode');
                    var countdown=60;
                    settime(obj,countdown,phone);
                } else {
                	toaster.clear('*');
                    toaster.pop('error', '', result.msg);
                }
                $scope.$apply();
            });
	}

	//验证码倒计时
    function settime(obj,countdown,phone) {
        if (countdown == 0) {
            obj.removeAttr("disabled");
            obj.html("获取验证码");
            countdown = 60;
            return;
        } else {
            obj.html("重新发送" + countdown);
            obj.attr("disabled", true);
            countdown--;
        }
        setTimeout(function() {
            settime(obj,countdown)
        },1000)
    }

	//输入状态交互
	var phoneNumber = document.getElementById('phoneNumber');
	var phoneCode = document.getElementById('phoneCode');
	$scope.check = function(){
		var number = phoneNumber.value;
		var code = phoneCode.value;
		//console.log(code);
		(code.length==6)? $scope.submitBtn = false:$scope.submitBtn = true;
		(code.length>0)? $scope.codeNotpass = false:$scope.codeNotpass = true;
	}

	//首次打开页面验证身份
	$scope.test = function(){
		$scope.submitBtn = true
		$scope.number = phoneNumber.value;
		$scope.code = phoneCode.value;
		$scope.param = {
			phone:$scope.number,
			smsCode:$scope.code
		}
		$.ajax({
			type: 'POST',
            dataType: 'json',
			contentType:'application/json;charset=UTF-8',
			url : 'dac/userManager/identifyingCode',
			data: angular.toJson($scope.param)
		}).then(function(result){
			console.log(result);
			if(result.httpCode ==200){//成功
                toaster.clear('*');
                toaster.pop('success','','验证成功！');
                $scope.pass = false;//隐藏弹层
                 $scope.$apply();
                var ss = sessionStorage.setItem('pass',$scope.pass);
           }else{
            	$scope.codeNotpass = true;
                phoneCode.value = '';
                $scope.$apply();
            }
		});
	};


	$scope.test1 = function(){

        $scope.upload = false;
        $scope.$apply();
		//alert(2);
		
		$scope.param.type='2';
		$.ajax({
		type: 'POST',
		dataType: 'json',
		contentType:'application/json;charset=UTF-8',
		url : 'dac/authm/cardAuth/importSchedule',
		data: angular.toJson($scope.param)
		}).then(function(result){
		document.getElementById('/dac/authm/cardAuth/importauthexcel').value ='';//为了使添加表时，input的value都是改变的先清空
		$scope.param.type='0';
		});


	};
	$scope.test2 = function(){
        $scope.upload = true;
		
        var progressInterval = setInterval(function(){
			$scope.param.timeStamp=$scope.time;
			$.ajax({
				type: 'POST',
				dataType: 'json',
				contentType:'application/json;charset=UTF-8',
				url : 'dac/authm/cardAuth/importSchedule',
				data: angular.toJson($scope.param)
			}).then(function(result){
				if(result.httpCode ==200){//成功
					 $scope.upload = true;
					console.log(result.data)
					var successCount = result.data.succecount;
					var faileCount = result.data.failcount;
					var totalCount = result.data.total;
					if(successCount+faileCount == totalCount){
						clearInterval(progressInterval);
						$("#currentProgress").attr("style","width:"+480+"px;height: 18px;background-color: #2c81ba;");
						$("#successCount").html(successCount);
						$("#successCountfaileCount").html(successCount+faileCount);
						$("#faileCount").html(faileCount);
						$("#totalCount").html(totalCount);
						
					}else{
						//var divWidth = $("#currentProgress").width();
						var newdivWidth = (successCount+faileCount) / totalCount * 480
						//divWidth = divWidth +10;
						$("#currentProgress").attr("style","width:"+newdivWidth+"px;height: 18px;background-color: #2c81ba;");
						$("#successCount").html(successCount);
						$("#successCountfaileCount").html(successCount+faileCount);
						$("#faileCount").html(faileCount);
						$("#totalCount").html(totalCount);
					}
					
	
	
				}else{
					$scope.$apply();
				}
			});
		
		
           
		}, 1000);

        // $scope.$apply();

	}

	//导入数据excel表格数据的操作
	$scope.change = function(id){
        $("#alertModel").click();

        $scope.upload = true;
        $scope.time = new Date().getTime();
		$scope.loadding = true;
		var _thisUrl = id + "?timeStamp="+$scope.time;
		var fileObj = document.getElementById(id).files[0];
//		console.log(_thisUrl);
		if(typeof (fileObj) == "undefined" || fileObj.size <= 0) {
               alert("请选择Excel类型文件");
               return;
           }else{
               var formFile = new FormData(); 
               formFile.append("excel", fileObj); //加入文件对象
               var data = formFile;
            $scope.upload = false;
               $.ajax({
                    url: _thisUrl,
                    data: data,
                    type: "post",
                    dataType: "json",
                    cache: false,//上传文件无需缓存
                    processData: false,//用于对data参数进行序列化处理 这里必须false
                    contentType: false, //必须
                    success: function (result) {
                    	$scope.loadding = false;
                    	console.log(result);
                        if(result.httpCode==200){
                        	// alert('上传成功');
                            // $scope.upload = false;
                            // $scope.$apply();
							
                        }else{
                        	alert(result.msg);
                        	document.getElementById(id).value = '';//为了使添加表时，input的value都是改变的先清空
                        }
                   },
                   error:function(){
                   		$scope.loadding = false;
                   		toaster.clear("*");
                        toaster.pop('error','',result.msg);
                   }
               })

           }     

	}

} ]);


	

		
		




